package ferp.android.dialogs;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.views.EnhancedTextView;
import ferp.core.game.Game;
import ferp.core.game.Settings;
import ferp.core.player.Profile;
import ferp.core.tutorial.Scenario;

/**
 * User: olegs
 * Date: 19/05/2014 13:29
 */
public class IntroductionDialog extends Dialog
{
  public interface Listener
  {
    void onCancel(int requestCode);
  }

  public static IntroductionDialog create(Context context, Listener listener, Profile profile, int requestCode)
  {
    return profile == null || profile.game() == null ? null : new IntroductionDialog(context, listener, profile, requestCode);
  }

  private IntroductionDialog(Context context, final Listener listener, Profile profile, final int requestCode)
  {
    super(context, R.layout.dialog_introduction);

    Game g = profile.game();

    settings = profile.settings;
    scenario = g.scenario();

    title.setText(Scenario.get(g.scenario).name(settings.locale));

    flipper = (ViewFlipper)findViewById(R.id.dlg_introduction_flipper);
    page0 = (ScrollView)findViewById(R.id.dlg_introduction_page_0);
    page1 = (ScrollView)findViewById(R.id.dlg_introduction_page_1);
    paging = (TextView)findViewById(R.id.dlg_introduction_paging);
    back = (Button)findViewById(R.id.dlg_introduction_button_back);
    next = (Button)findViewById(R.id.dlg_introduction_button_next);

    flipper.setMeasureAllChildren(true);

    setOnCancelListener(new OnCancelListener()
    {
      @Override
      public void onCancel(DialogInterface dialogInterface)
      {
        // avoid double processing
        if (flipper.isClickable())
        {
          flipper.setClickable(false);
          listener.onCancel(requestCode);
        }
      }
    });

    // set onClick listeners to cancel the dialog,
    // each text view's listener must be set separately since ScrollView doesn't fire onClick event
    setOnClickListener(page0.getChildAt(0));
    setOnClickListener(page1.getChildAt(0));

    back.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View view)
      {
        if (view.isEnabled() && animations == 0)
          back();
      }
    });

    next.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View view)
      {
        if (view.isEnabled() && animations == 0)
          next();
      }
    });

    setOnShowListener(new OnShowListener()
    {
      @Override
      public void onShow(DialogInterface dialogInterface)
      {
        // restore clickable state
        flipper.setClickable(true);
      }
    });
  }

  public void prepare()
  {
    animations = 0;
    page = 0;
    showPage();
  }

  private void setOnClickListener(View v)
  {
    v.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View view)
      {
        cancel();
      }
    });
  }

  private void setAnimationListener(Animation a)
  {
    a.setAnimationListener(new Animation.AnimationListener()
    {
      @Override
      public void onAnimationStart(Animation animation) {/* nothing to do */}
      @Override
      public void onAnimationRepeat(Animation animation) {/* nothing to do */}

      @Override
      public void onAnimationEnd(Animation animation) {--animations;}
    });
  }

  private void back()
  {
    Context context = getContext();

    animations = MAXIMUM_ANIMATIONS;

    flipper.setInAnimation(context, R.anim.flip_in_from_left);
    flipper.setOutAnimation(context, R.anim.flip_out_to_right);

    // set animation listeners to reset 'busy' flag
    setAnimationListener(flipper.getInAnimation());
    setAnimationListener(flipper.getOutAnimation());

    --page;
    showPage();
  }

  private void next()
  {
    Context context = getContext();

    animations = MAXIMUM_ANIMATIONS;

    flipper.setInAnimation(context, R.anim.flip_in_from_right);
    flipper.setOutAnimation(context, R.anim.flip_out_to_left);

    // set animation listeners to reset 'busy' flag
    setAnimationListener(flipper.getInAnimation());
    setAnimationListener(flipper.getOutAnimation());

    ++page;
    showPage();
  }

  private void showPage()
  {
    View v = flipper.getChildAt(0);
    int pages = scenario.pages();

    // swap steps (current page can be reused for the next one)
    flipper.removeViewAt(0);
    flipper.addView(v);

    // show second page
    ScrollView second = v == page0 ? page0 : page1;
    EnhancedTextView tv = (EnhancedTextView)second.getChildAt(0);

    tv.setText(scenario.getPageText(settings.locale, page));

    flipper.showNext();

    // set paging state
    paging.setText((page + 1) + "/" + pages);

    GUI.enableButton(back, page > 0);
    GUI.enableButton(next, page < pages - 1);
  }

  // maximum number of animations when flipping pages
  private static final int MAXIMUM_ANIMATIONS = 2;

  // widgets
  private final ViewFlipper flipper;
  private final TextView paging;
  private final ScrollView page0;
  private final ScrollView page1;
  private final Button back;
  private final Button next;

  // current settings and scenario
  private Settings settings;
  private Scenario scenario;

  // scenario's current page
  private int page;

  // 'animations' counter to avoid animations overlap when pressing 'back'/'next' buttons too fast
  private int animations;
}
